<template>
  <section>
    <sticky :className="'sub-navbar draft'">
      <el-button v-loading="isSubmitLoading" style="margin-left: 10px;" type="success" @click="submit">保存 </el-button>
    </sticky>
    <div class="app-container is-form">
      <el-form :model="form" ref="form" :rules="rules" label-width="125px" v-loading="loading" >
        <el-row class="createPost-main-container">
          ## for(item in entity.generalList) {
          ## if(item.isId == true){continue;}
          ## if(item.name == "deleted" ){continue;}
          ## if(item.name == "createAt" ){continue;}
          ## if(item.name == "creatorId" ){continue;}
          ## if(item.name == "modifyAt" ){continue;}
          ## if(item.name == "modifierId" ){continue;}
          <el-form-item label="${item.displayName}" prop="${item.name}">
            <el-input v-model="form.${item.name}" style="width:300px;"></el-input>
          </el-form-item>
          ## }
        </el-row>
      </el-form>
    </div>
  </section>
</template>
<script>
  import Sticky from '@/components/Sticky' // 粘性header组件
  import {require,pattern} from '../../utils/rules';
  import {saveOne,getDetail} from "../../api/${entity.code}";
  export default {
    name:"${entity.name}",
    components: { Sticky },
    data() {
      return{
        pageTitle: '新增${entity.displayName}',
        loading: false,
        isSubmitLoading: false,
        form: {
          ## for(item in entity.generalList) {
            ## if(item.isId == true){continue;}
            ## if(item.name == "deleted" ){continue;}
            ## if(item.name == "createAt" ){continue;}
            ## if(item.name == "creatorId" ){continue;}
            ## if(item.name == "modifyAt" ){continue;}
            ## if(item.name == "modifierId" ){continue;}
            ${item.name}: ${item.javaType == "String"?  "\"\"" : "null" },
          ## }
        },
        rules:{}
      }
    },
    methods: {
      submit() {
        this.$refs['form'].validate(async (success) => {
          if(success){
            let resp = await saveOne({entity:this.form})
            if(this.qUtil.validResp(resp)){
              this.$message({ message: resp.msg, type: 'success' });
              this.$store.dispatch('tagsView/delCurrentViews',{
                view: this.$route,
                $router: this.$router,
                nextView: {name: "${entity.name}List"}
              })
            }
          }
        })
      },
      async loadModel(){
        let id  =   this.$route.query.id || 0
        if(id > 0){
          let resp = await getDetail({id:id})
          if(this.qUtil.validResp(resp)){
            this.pageTitle = "编辑${entity.displayName}"
            this.form = this.baseUtil.modelCopy(resp.data.model)
          }
        }else {
        }
      },
    },
    mounted() {
      this.loadModel()
    }
  }
</script>
